package MyRemoteSorter;

import java.util.ArrayList;
import java.util.Random;
import MyRemoteSorter.Auto;

public class SortClient {
	
	
	Random kans = new Random();
	Comparable[] sortedResult;
	int aantal;
	Class<?> type;

	public SortClient() {
		this(1,1000);
	}
	
	public SortClient(int type, int aantal) {
		//this.type = type;
		switch(type)
		{
		 default:
		 		this.type=Integer.class;
		 case 1:
			 this.type=String.class;
		 case 2:
			 this.type=Auto.class;
		}
		this.aantal=aantal;
		prepareSort();
	}
	private void prepareSort() {
		try {
			Comparable[] list = generateRandom(aantal, type);
			System.out.println(System.currentTimeMillis());
			System.out.println("aantal:"+aantal+" - type:"+type);
			System.out.println("generated list: ");
			printComparable(list);
			
			Comparable[][] splicedLists = quickSpliceList(list);
			System.out.println("spliced list: ");
			printComparable(splicedLists);
			
			ListJoiner joiner = new ListJoiner();
			for (Comparable[] lst : splicedLists) {
				SorterThread sortering = new SorterThread(lst, joiner);
				sortering.start();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Comparable[] generateRandom(int length, Class<?> type) {
		if (type.equals(String.class)) {
			return generateRandomStrings(length);
		} else if (type.equals(Integer.class)) {
			return generateRandomIntegers(length);
		} else if (type.equals(Auto.class)) {
			return generateRandomAutos(length);
		} else {
			return null;
		}
	}

	private Comparable[] generateRandomAutos(int length) {
		Auto[] generatieLijst = new Auto[length];
		for (int i = 0; i < length; i++) {
			int jaar = 2012 - kans.nextInt(50);
			String kenteken = generateString(2) + "-" + kans.nextInt(99) + "-"
					+ generateString(2);
			generatieLijst[i] = new Auto(generateString(5), generateString(5),
					jaar, kenteken);
		}
		return generatieLijst;
	}

	private Comparable[] generateRandomIntegers(int length) {
		Integer[] generatieLijst = new Integer[length];
		for (int i = 0; i < length; i++) {
			generatieLijst[i] = (Integer) kans.nextInt(length * 10);
		}
		return generatieLijst;
	}

	public Comparable[] generateRandomStrings(int lengte) {
		Comparable[] generatieLijst = new String[lengte];
		for (int arrayLengte = 0; arrayLengte < lengte; arrayLengte++) {
			String woord = generateString(8);
			generatieLijst[arrayLengte] = woord;
		}
		return generatieLijst;
	}

	private String generateString(int maxLengte) {
		String woord = "";
		String alphabet = "abcdefghijklmnopqrstuvwxyz";
		for (int stringLengte = 0; stringLengte < maxLengte; stringLengte++) {
			woord = woord + alphabet.charAt(kans.nextInt(alphabet.length()));
		}
		return woord;
	}

	private Comparable[][] quickSpliceList(Comparable[] bigLijst) {
		int sorteerders = (int) Math.log10(bigLijst.length)+1;
		int deler = (int) Math.round(Math.ceil(((bigLijst.length) / sorteerders)));
		System.out.println("deler = " + deler + " sorteerders = " + sorteerders);
		Comparable[][] verzamelLijst = new Comparable[sorteerders][];
		int totaalIndex = 0;
		for (int delen = 0; delen < sorteerders; delen++) 
		{
			ArrayList<Comparable<Comparable>> deelLijst = new ArrayList<Comparable<Comparable>>();
			if((Math.round(bigLijst.length) % sorteerders != 0)&&delen==sorteerders-1)
			{
				deler = deler + (Math.round(bigLijst.length) % sorteerders);
			}
			for (int index = 0; index < deler; index++) {
				deelLijst.add(bigLijst[totaalIndex]);
				totaalIndex++;
			}
			verzamelLijst[delen] = deelLijst.toArray(new Comparable[0]);
		}
		return verzamelLijst;
	}
	private Comparable[][] smartSpliceList(Comparable[] bigLijst)
	{
	//	TODO: slimme versie voor spicen implementeren(Iteratie 2)
		return null;
	}

	private void printComparable(Comparable[] numbers) {
		for (int i = 0; i < numbers.length; i++) {
			System.out.println(i + " " + numbers[i]);
		}
	}

	private void printComparable(Comparable[][] numbers) {
		for (int i = 0; i < numbers.length; i++) {
			printComparable(numbers[i]);
			}
	}

	

}
